package br.ucb.cm.main.ai.algorithm;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import br.ucb.cm.board.bean.Location;
import br.ucb.cm.board.bean.Move;
import br.ucb.cm.board.bean.Piece;
import br.ucb.cm.board.util.BoardUtil;
import br.ucb.cm.config.bean.PieceConfig;
import br.ucb.cm.main.ai.AIAlgorithm;
import br.ucb.cm.main.common.GeneralConstants.PlayerEnum;

public class RandomAI implements AIAlgorithm{

	@Override
	public Move doMove(Map<Location, PieceConfig> pieceMap, PlayerEnum player) {
		List<Move> moves = new ArrayList<Move>();
		Map<Location, PieceConfig> map = pieceMap;
		
		for (Entry<Location, PieceConfig> entry : map.entrySet()) {
			Location location = entry.getKey();
			PieceConfig piece = entry.getValue();
			if (BoardUtil.getOwner(piece) == player) {
				moves.addAll(BoardUtil.getMovesForPiece(new Piece(piece, location, player), map));
				moves.addAll(BoardUtil.getAttacksForPiece(new Piece(piece, location, player), map));
			}
		}
		
		if(moves.size() > 0){
			int index = (int) (Math.random() * moves.size());
			return moves.get(index);
		}
		else
			return null;
	}

}
